﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace AppDummy
{

    /// <summary>
    /// A dummy driver module that 
    /// 1. opens and register a dummy ports
    /// 2. sends periodic notifications  (in Work())
    /// 3. sends back responses to received echo requests (in OnOperationInvoke())
    /// </summary>

    [System.AddIn.AddIn("DriverDummy")]
    public class DriverDummy :  ModuleBase
    {

        Port dummyPort;

        public override void Start()
        {
            logger.Log("Started: {0}", ToString());
 
            // ..... initialize the list of roles we are going to export
            List<View.VRole> listRole = new List<View.VRole>();
            
            RoleDummy dummyRole = new RoleDummy();
            listRole.Add(dummyRole);

            //.................instantiate the port
            View.VPortInfo portInfo = GetPortInfoFromPlatform("port");
            dummyPort = InitPort(portInfo);

            //..... bind the port to roles and delegates
            BindRoles(dummyPort, listRole, OnOperationInvoke);

            //.................register the port after the binding is complete
            RegisterPortWithPlatform(dummyPort);

            Work();
        }

        public override void Stop()
        {
            Finished();
        }


        /// <summary>
        /// Sit in a loop and send notifications 
        /// </summary>
        public void Work()
        {
            int counter = 0;
            while (true)
            {
                counter++;

                IList<View.VParamType> retVals = new List<View.VParamType>();
                retVals.Add(new ParamType(ParamType.SimpleType.integer, "", counter, "responsenum"));

                dummyPort.Notify(RoleDummy.RoleName, RoleDummy.OpEchoSubName, retVals);

                System.Threading.Thread.Sleep(1 * 10 * 1000);
            }
        }

        /// <summary>
        /// The demultiplexing routing for incoming operations
        /// </summary>
        /// <param name="message"></param>
        public IList<View.VParamType> OnOperationInvoke(string roleName, String opName, IList<View.VParamType> args)
        {
            switch (opName.ToLower())
            {
                case RoleDummy.OpEchoName:
                    int payload = (int)args[0].Value();
                    logger.Log("{0} Got EchoRequest {1}", this.ToString(), payload.ToString());

                    List<View.VParamType> retVals = new List<View.VParamType>();
                    retVals.Add(new ParamType(ParamType.SimpleType.integer, "", -1 * payload, "responsenum"));

                    return retVals;

                default:
                    logger.Log("Invalid operation: {0}", opName);
                    return null;
            }
        }

        /// <summary>
        ///  Called when a new port is registered with the platform
        ///        the dummy driver does not care about other ports in the system
        /// </summary>
        public override void PortRegistered(View.VPort port) {}

        /// <summary>
        ///  Called when a new port is deregistered with the platform
        ///        the dummy driver does not care about other ports in the system
        /// </summary>
        public override void PortDeregistered(View.VPort port) { }
    }
}